package com.jqbd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jqbd.common.R;
import com.jqbd.pojo.Education;
import com.jqbd.service.EducationService;
import com.jqbd.util.JsonResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author yuan
 * @date 2022/11/11 9:06
 * @description education 控制层
 */

@RestController
@RequestMapping("education")
public class EducationController {

    @Resource
    private EducationService educationService;

    /**
     * 查询所有学历信息
     * @return
     */
    @RequestMapping("findAll")
    public List<Education> findAll() {
        return educationService.findAll();
    }


    /**
     * 分页查询学历信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("findByPage")
    public JsonResult<Education> findByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "3") Integer pageSize
    ) {
        PageHelper.startPage(pageNum, pageSize);

        List<Education> educationList = educationService.findAll();

        PageInfo<Education> educationPageInfo = new PageInfo<>(educationList);

        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setCount(educationPageInfo.getTotal());
        jsonResult.setData(educationPageInfo.getList());

        jsonResult.setCode(0);

        return jsonResult;
    }

    /**
     * 添加学历信息
     *
     * @param education  学历对象
     * @param request  请求
     * @param response 响应
     */
    @RequestMapping("add")
    public R add(Education education, HttpServletResponse response, HttpServletRequest request) throws IOException {

        System.out.println(education);

        int add = educationService.add(education);

        // 重定向到后端
//        response.sendRedirect(request.getContextPath() + "/home.html");
        return add>0 ? R.ok():R.error();
    }


    /**+
     * 修改学历信息
     * @param education 学历对象
     * @return
     */
    @RequestMapping("update")
    public R update(Education education) {
        int i = educationService.update(education);
        return i>0?R.ok():R.error();
    }

    /**
     * 删除单个学历信息
     * @param educationId 学历编号
     * @return
     */
    @RequestMapping("deleteById")
    public String deleteById(Integer educationId) {

        educationService.deleteById(educationId);

        return "success";
    }


    /**
     * 批量删除学历信息
     * @param ids 学历编号数组对象
     * @return
     */
    @RequestMapping("deleteByIds")
    public R deleteByIds(Integer[] ids) {
        int i = educationService.deleteByIds(ids);
        return i>0?R.ok():R.error();
    }

    /**
     * 导入 Excel 学历信息
     * @param file
     * @return
     */
    @RequestMapping("upload")
    public JsonResult upload(MultipartFile file) {
        // 判断
        if (file != null) {

            // System.out.println("上传的文件名：" + file.getOriginalFilename());

            educationService.save(file);

            JsonResult jsonResult = JsonResult.ok();

            return jsonResult;
        }

        throw new RuntimeException();
    }
}
